package org.eae.eldf.dao.module.impl;

import java.util.ArrayList;
import java.util.List;

import org.eae.eldf.dao.module.IModulePurviewDao;
import org.eae.eldf.dto.module.ModulePurviewQueryDto;
import org.eae.eldf.dto.module.ModuleRolePurviewDto;
import org.eae.eldf.entity.module.ModuleMenu;
import org.eae.eldf.entity.module.ModulePurview;
import org.eae.eldf.framework.dao.JdbcDaoImpl;
import org.eae.eldf.framework.pager.Pagination;
import org.eae.eldf.framework.utils.StringUtil;
import org.springframework.stereotype.Repository;

@Repository
public class ModulePurviewDaoImpl extends JdbcDaoImpl implements
		IModulePurviewDao {

	// 通过模块权限名称模糊查找模块权限
	@Override
	public Pagination<ModulePurview> pageModulePurviewList(
			Pagination<ModulePurview> pager, ModulePurviewQueryDto modulePurviewQueryDto) {
		StringBuffer sql = new StringBuffer("select * from module_purview t ");
		List<Object> params = new ArrayList<Object>();
		if (modulePurviewQueryDto!=null) {
		if (modulePurviewQueryDto.getModuleMenuId()!=null) {
			sql.append("where t.module_menu_id = ?");
			params.add(modulePurviewQueryDto.getModuleMenuId());
		}
		}
		sql.append(" order by module_purview_id desc");
		return this.queryListByPage(pager, sql.toString(), params,
				ModulePurview.class);
	}

	@Override
	public Pagination<ModulePurview> pageModuleListByMid(
			Pagination<ModulePurview> pager, long moduleMenuId) {
		StringBuffer sql = new StringBuffer("select * from module_purview t ");
		List<Object> params = new ArrayList<Object>();
		sql.append("where t.module_menu_id = ?");
		params.add(moduleMenuId);
		sql.append(" order by module_purview_id desc");

		return this.queryListByPage(pager, sql.toString(), params,
				ModulePurview.class);
	}

/*	@Override
	//通过产品类别ID查询菜单权限生产成菜单树
	public List<ModuleRolePurviewDto> queryListByProdcategoryId(Long categoryId) {
		String sql = "select  concat('pur_',a.module_purview_id) as pid,a.pur_name as name,a.module_menu_id as parentid," +
				"if(a.module_purview_id in (select b.module_purview_id from prod_category_purview b where b.prod_category_id=? )" +
				",1,0) as isChecked from module_purview a order by a.module_purview_id asc";
		return this.queryBySQL(sql,new Object[]{categoryId}, ModuleRolePurviewDto.class);
	}*/

	/* (non-Javadoc)
	 * @see 通过模块菜单列表查询模块权限列表
	 */
	@Override
	public List<ModulePurview> queryByMenuList(List<ModuleMenu> list) {
		StringBuffer sql=new StringBuffer();
		List<Object> params = new ArrayList<Object>();
		sql.append("select * from module_purview where module_menu_id in(");
		for (ModuleMenu moduleMenu : list) {
			if (list.indexOf(moduleMenu) == (list.size() - 1)) {
				sql.append("?");
				params.add(moduleMenu.getModuleMenuId());
			} else {
				sql.append("?,");
				params.add(moduleMenu.getModuleMenuId());
			}
		}
		sql.append(") ");
		return this.queryBySQL(sql.toString(), params, ModulePurview.class);
	}

	
	
	
	
	
	

}
